package com.uchengwang.ui.cm_ui.me.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnHttpResultListener;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.rychat.adapter.RyGroupMemberAdapter;
import com.uchengwang.ui.cm_ui.rychat.bean.RyUserBean;
import com.uchengwang.ui.cm_ui.rychat.manager.RyGroupManager;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.List;

/**
 * Created by U城网 on 2017/7/5.
 * 更多成员 群成员[和群相关]
 */

public class MoreMemebersActivity extends BaseWithTitleActivity{
    private RecyclerView recycleView;
    private PtrClassicFrameLayout mSwipeRefreshLayout;
    private RecyclerAdapterWithHF mAdapter;
    private RyGroupMemberAdapter adapter;
    private long groupId, owerId;//群ID 群主ID
    private int type = 9;//9查看群成员 8管理权转让 7移除成员
    private boolean isDelete = false;//是否是移除状态
    private int page =1, totalPages;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_ustudy_2);
        type = getIntent().getIntExtra(BaseConfig.TYPE, 0);
        groupId = getIntent().getLongExtra(BaseConfig.GROUP_ID, 0);
        owerId = getIntent().getLongExtra(BaseConfig.UID, 0);

        switch (type){
            case 8:
                setTitle("管理权转让");
                break;
            case 9:
            case 7:
                setTitle("成员");
                if (owerId == UCommonUtil.getUtil().getId()){
                    setRightText("移除", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            //显示移除成员
                            if (!isDelete){
                                isDelete = true;
                                adapter.setType(7);
                                setRightText("取消", this);
                            }else {
                                isDelete = false;
                                adapter.setType(9);
                                setRightText("移除", this);
                            }
                        }
                    });
                }
                break;
        }

        initView();
    }

    private void initView() {
        recycleView = (RecyclerView) findViewById(R.id.u_study_recycleView);
        mSwipeRefreshLayout = (PtrClassicFrameLayout) findViewById(R.id.mRefreshLayout);

        initData();

    }

    private void initData(){
        recycleView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new RyGroupMemberAdapter(this, owerId, groupId, type);
        mAdapter = new RecyclerAdapterWithHF(adapter);
        recycleView.setAdapter(mAdapter);

        mSwipeRefreshLayout.setAutoLoadMoreEnable(true);
        mSwipeRefreshLayout.setLoadMoreEnable(true);

        mSwipeRefreshLayout.postDelayed(new Runnable() {

            @Override
            public void run() {
                mSwipeRefreshLayout.autoRefresh(true);
            }
        }, 150);

        mSwipeRefreshLayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                page =1;
                getMembers(true);
            }
        });

        mSwipeRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                page++;
                if (page <= totalPages){
                    //当前请求页大于总页数，不再请求
                    getMembers(false);
                } else {
                    mSwipeRefreshLayout.loadMoreComplete(false);
                }
            }
        });
    }


    /**
     * 获取群成员
     * */
    private void getMembers(final boolean isRefresh){
        RyGroupManager.getManager().getGroupMembers(this, groupId, page, 10, new OnHttpResultListener<RyUserBean>() {
            @Override
            public void onResult(int status, String msg, int total, List<RyUserBean> list) {
                totalPages = total;
                if (isRefresh){
                    mSwipeRefreshLayout.refreshComplete();
                }else {
                    mSwipeRefreshLayout.loadMoreComplete(true);
                }
                if (list == null || list.size() <1){
                    return;
                }

                if (isRefresh){
                    adapter.setData(list);
                }else {
                    adapter.getData().addAll(list);
                    adapter.notifyDataSetChanged();
                }

            }
        });


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        List<RyUserBean> list = adapter.getData();
        list.clear();
        list = null;
    }
}
